#    Rubatom
#    Copyright (c) 2014 Sebastian Dufner
#
#    This file is part of Rubatom.
#
#    Rubatom is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Rubatom is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'delegate'

module Rubatom
  class Template
    class Links < SimpleDelegator
      def self.deserialize(input)
        length = input.read_many(4)[0]

        new length.times.map {Link.deserialize(input)}
      end

      def serialize(output)
        output.write_many(size => 4)
        each {|link| link.serialize(output)}
      end
    end

    class Link
      def self.deserialize(input)
        new(*input.read_many(2, 2, 2, 2, 4, 2, 2, 2, 2))
      end

      def initialize(u1, z_master, y_master, x_master,
            u2, u3, z_slave, y_slave, x_slave)
        @u1, @z_master, @y_master, @x_master, @u2, @u3, @z_slave, @y_slave,
            @x_slave =
          u1, z_master, y_master, x_master, u2, u3, z_slave, y_slave, x_slave
      end
      attr_accessor :u1, :z_master, :y_master, :x_master, :u2, :u3, :z_slave,
          :y_slave, :x_slave

      def serialize(output)
        output.write_many(
              [
                u1, z_master, y_master, x_master,
                u2, u3, z_slave, y_slave, x_slave
              ] => [2, 2, 2, 2, 4, 2, 2, 2, 2])
      end
    end

  end
end
